package com.irdstudio.rsp.drools.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GzipEncodingFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        String gzipEnable = request.getHeader("gzip");
        // 判断是否前端有gzip压缩
        if (Boolean.valueOf(gzipEnable)) {
            HttpServletResponse response = (HttpServletResponse) resp;
            //response.addHeader("Content-Encoding", "gzip");
            chain.doFilter(new GzipRequestWrapper(request), resp);
        } else {
            chain.doFilter(req, resp);
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
